
************************************************
*Purpose: Replicate Figure 5 of the manuscript**
*Stata Version: 17
*By Marc van de Wardt (m.p.vande.wardt@vu.nl)
************************************************

**Install additional packages needed

ssc install blindschemes, replace
net install blindschemes_fix, replace from("http://digital.cgdev.org/doc/stata/MO/Misc")

**Set working directory

cd "C:\Users\LT-054912\surfdrive\Diffusion\dataverse files\"

**run do-file to calculate pathway scores (skip if you have already done so)

run "Stata commands to replicate figure a3 and a4.do"

**Open log files\

log using "Stata commands to replicate figure 5.smcl", replace

**Get learning pathway scores

preserve

use "pathway scores h4.dta", clear

keep if family_dynamic1=="Radical Right Populists"

bys country_ipol: egen max_path=max(pathways1)

rename pathways1 pathways1_h4 
rename max_path max_path_h4

save "Radical Right Populists_learn_pathway.dta", replace

restore

use "dataset for analysis.dta", clear

keep if family_dynamic1=="Radical Right Populists"

sort family_dynamic1 country_ipol elec_year
merge family_dynamic1 country_ipol elec_year using "pathway scores h1.dta", sort
drop if _merge==2
drop _merge

keep country_ipol elec_year wx_lag_votperfor_ipol family_dynamic1 totentry2 totentry2_ipol pathways1 lag_totentry2_ipol lag_votperfor_ipol votperfor_ipol pathways1

sort family_dynamic1 country_ipol elec_year
merge family_dynamic1 country_ipol elec_year using "Radical Right Populists_learn_pathway.dta", sort
drop if _merge==2
drop _merge

**H1

bys country_ipol: egen max_path_h1=max(pathways1)
bys country_ipol: tab elec_year if pathways1==max_path_h1
bys country_ipol: tab pathways1 if pathways1==max_path_h1

bys country_ipol: egen max_path_event_h1=max(pathways1) if totentry2_ipol!=0
bys country_ipol: egen max_path_event_h1_v2=max(max_path_event_h1)

drop max_path_event_h1
rename max_path_event_h1_v2 max_path_event_h1

bys country_ipol: egen max_path_event_noimp_h1=max(pathways1) if totentry2!=0 & totentry2!=.
bys country_ipol: egen max_path_event_noimp_h1_v2=max(max_path_event_noimp_h1)

drop max_path_event_noimp_h1
rename max_path_event_noimp_h1_v2 max_path_event_noimp_h1

**H4

bys country_ipol: egen max_path_event_h4=max(pathways1_h4) if totentry2_ipol!=0
bys country_ipol: egen max_path_event_h4_v2=max(max_path_event_h4)

drop max_path_event_h4
rename max_path_event_h4_v2 max_path_event_h4

bys country_ipol: egen max_path_event_noimp_h4=max(pathways1_h4) if totentry2!=0 & totentry2!=.
bys country_ipol: egen max_path_event_noimp_h4_v2=max(max_path_event_noimp_h4)

drop max_path_event_noimp_h4
rename max_path_event_noimp_h4_v2 max_path_event_noimp_h4

bys family_dynamic1 elec_year: egen wy_lag_totentry2_ipol=sum(lag_totentry2_ipol)
replace wy_lag_totentry2_ipol=wy_lag_totentry2_ipol-lag_totentry2_ipol

bys country: tab max_path_event_h1
bys country: tab elec_year if max_path_event_h1==pathways1

bys elec_year: egen famentry=sum(totentry2_ipol)
bys elec_year: egen famentrylag=sum(lag_totentry2_ipol) 
bys elec_year: egen famvotlag=sum(lag_votperfor_ipol) 
bys elec_year: egen famvot=sum(votperfor_ipol)
bys elec_year: egen mean_path=mean(pathways1) 
bys elec_year: egen famentry_noimp=sum(totentry2)

gen votperfor=votperfor
replace votperfor=. if totentry2==.

bys elec_year: egen famvot_noimp=sum(votperfor)

rename pathways1 path_h1
rename pathways1_h4 path_h4

**Here we implement that pathway scores are only used in years where the family entered in the focal country
replace path_h1=. if totentry2_ipol==0
replace path_h4=. if totentry2_ipol==0

gen elec_year2=elec_year+0.4

**Median pathway score

bys elec_year: egen med_path_h1=median(path_h1)
bys elec_year: egen med_path_h4=median(path_h4)

twoway bar med_path_h1 elec_year, barwidth(0.3) col(black)  ///
|| bar med_path_h4 elec_year2, barwidth(0.3) col(gs10) yline(0) ///
ytitle("Pathway median score") xla(1960(5)2020) ///
legend(order(1 "Median pathway score emulation" 2 "Median pathway score learning")  pos(6)) ///
scheme(plottig)  name(e, replace) 

**Standardized entry

egen std_famentry=std(famentry)
egen std_famvot=std(famvot)

sort country elec_year
twoway line std_famentry  elec_year if country_ipol=="France" ,  lcolor(black)  ///
|| line std_famvot elec_year if country_ipol=="France", lcolor(gs10)   ///
ytitle("Standardized values") ///
yla(-2 "-2  " -1 "-1  " 0 "0  " 1 "1  " 2 "2  " 3 "3  " ) ///
xtitle("") xla(1960(5)2020) scheme(plottig) legend(order(1 "Party family entry (most recent election)" 2 "Vote share of party family (most recent election)")  pos(6)) name(a, replace)

graph combine a e, scheme(plottig) col(1)
graph export "Figure5.tif", replace

log close